package com.ymatou.iapi.couponservice.testcase;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.couponservice.parameter.ConversionCouponBean;
import com.ymatou.iapi.couponservice.service.ConversionCouponCall;
import com.ymatou.iapi.m.couponservice.parameter.CreateBean;
import com.ymatou.iapi.m.couponservice.parameter.CreateCouponBean;
import com.ymatou.iapi.m.couponservice.service.CreateCall;
import com.ymatou.iapi.m.couponservice.service.CreateCouponCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.CouponCallService;
import com.ymttest.business.service.CouponServiceCallService;
import com.ymttest.business.service.CouponServiceMCallService;
import com.ymttest.database.model.YmtCoupon;
import com.ymttest.database.model.YmtCouponBatch;
import com.ymttest.database.model.YmtCouponConversionBill;
import com.ymttest.database.model.YmtCouponConversionCode;
import com.ymttest.database.model.YmtCouponConversionRecord;
import com.ymttest.database.model.YmtCouponPrivateUserBound;
import com.ymttest.database.model.YmtCouponScenario;
import com.ymttest.database.model.YmtCouponSetting;
import com.ymttest.database.model.YmtCouponValue;
import com.ymttest.database.sqlwapper.YmtCouponBatchWapper;
import com.ymttest.database.sqlwapper.YmtCouponConversionBatchRelationWapper;
import com.ymttest.database.sqlwapper.YmtCouponConversionBillWapper;
import com.ymttest.database.sqlwapper.YmtCouponConversionCodeWapper;
import com.ymttest.database.sqlwapper.YmtCouponConversionRecordWapper;
import com.ymttest.database.sqlwapper.YmtCouponPrivateUserBoundWapper;
import com.ymttest.database.sqlwapper.YmtCouponScenarioWapper;
import com.ymttest.database.sqlwapper.YmtCouponSettingWapper;
import com.ymttest.database.sqlwapper.YmtCouponValueWapper;
import com.ymttest.database.sqlwapper.YmtCouponWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.TestDataManager;
/**
*兑换优惠券
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_ConversionCoupon{
	private static ConversionCouponBean conversioncouponBean;
	private static ConversionCouponCall conversioncouponCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("兑换优惠券");
	}

	@Before
	public void caseUp() {
		conversioncouponBean=new ConversionCouponBean();
		conversioncouponCall=new ConversionCouponCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(conversioncouponCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ConversionCoupon_001() {
		Logger.start(true,"兑换优惠券-随机券");
		try {
			//生成批次及兑换码
			CouponServiceMCallService couponMCallService = new CouponServiceMCallService();
			CreateCouponBean createCouponBean = couponMCallService.setDefaultPlatformCreateCouponBeanForConversion();

			CreateCouponCall createCouponCall = couponMCallService.createPlatformCoupon(createCouponBean);

			List<String> batchCodes = new ArrayList<>();
			batchCodes.add(createCouponCall.getBatchCode());

			CreateBean createBean = couponMCallService.setDefaultRandomCreateBean(batchCodes);
			CreateCall createCall = couponMCallService.create(createBean);
		
			// 等待兑换码生成
			YmtCouponConversionBill bill = new YmtCouponConversionBillWapper()
					.selectByBillName(createBean.getBillName()).get(0);
			List<YmtCouponConversionCode> ymtCouponConversionCodes = new YmtCouponConversionCodeWapper().selectByConversionBillId(bill.getConversionbillid());
			
			int flag=0;
			while(ymtCouponConversionCodes.size()<createBean.getGenerateQuantity() && flag<500)
			{
				flag++;
				Thread.sleep(200);
				ymtCouponConversionCodes = new YmtCouponConversionCodeWapper().selectByConversionBillId(bill.getConversionbillid());	
			}
			
			Logger.verifyEquals(createBean.getGenerateQuantity(), ymtCouponConversionCodes.size(), "验证数据Ymt_CouponConversionCode中生成相应记录");
			
			// 设置值
			conversioncouponBean.setUserId(Integer.valueOf(EnvSetup.getData("BuyUserId")));
			conversioncouponBean.setConversionCode(ymtCouponConversionCodes.get(0).getConversioncode());
			conversioncouponBean.setAppId("Ymt");
			conversioncouponBean.setRequestId("12344");
			conversioncouponBean.setUserType(0);
			
			conversioncouponCall.setData(conversioncouponBean);
			conversioncouponCall.callService();	
			
			VerifyReturn(createCouponBean,createBean);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ConversionCoupon_002() {
		Logger.start(true,"兑换优惠券-自定义券");
		try {
			//生成批次及兑换码
			CouponServiceMCallService couponCallService = new CouponServiceMCallService();
			CreateCouponBean createCouponBean = couponCallService.setDefaultPlatformCreateCouponBeanForConversion();
			createCouponBean.setCouponNum(3000);
			CreateCouponCall createCouponCall = couponCallService.createPlatformCoupon(createCouponBean);

			CouponServiceMCallService mCouponserviceCallService = new CouponServiceMCallService();

			List<String> batchCodes = new ArrayList<>();
			batchCodes.add(createCouponCall.getBatchCode());

			CreateBean createBean = mCouponserviceCallService.setDefaultCustomCreateBean(batchCodes);
			CreateCall createCall = mCouponserviceCallService.create(createBean);
		
			// 等待兑换码生成
			YmtCouponConversionBill bill = new YmtCouponConversionBillWapper()
					.selectByBillName(createBean.getBillName()).get(0);
			List<YmtCouponConversionCode> ymtCouponConversionCodes = new YmtCouponConversionCodeWapper().selectByConversionBillId(bill.getConversionbillid());
			
			int flag=0;
			while(ymtCouponConversionCodes.size()<1 && flag<500)
			{
				flag++;
				Thread.sleep(200);
				ymtCouponConversionCodes = new YmtCouponConversionCodeWapper().selectByConversionBillId(bill.getConversionbillid());	
			}
			
			Logger.verifyEquals(createBean.getGenerateQuantity(), ymtCouponConversionCodes.size(), "验证数据Ymt_CouponConversionCode中生成相应记录");
			
			// 设置值
			conversioncouponBean.setUserId(Integer.valueOf(EnvSetup.getData("BuyUserId")));
			conversioncouponBean.setConversionCode(ymtCouponConversionCodes.get(0).getConversioncode());
			conversioncouponBean.setAppId("Ymt");
			conversioncouponBean.setRequestId("12344");
			conversioncouponBean.setUserType(0);
			
			conversioncouponCall.setData(conversioncouponBean);
//			conversioncouponCall.callService();	
//			
//			conversioncouponBean.setUserId(TestDataManager.getRandomIntNum(8));
//			conversioncouponCall.setData(conversioncouponBean);
//			conversioncouponCall.callService();	
//			
//			conversioncouponBean.setUserId(TestDataManager.getRandomIntNum(8));
//			conversioncouponCall.setData(conversioncouponBean);
//			conversioncouponCall.callService();	
//			
//			conversioncouponBean.setUserId(TestDataManager.getRandomIntNum(8));
//			conversioncouponCall.setData(conversioncouponBean);
//			conversioncouponCall.callService();	
//			
//			conversioncouponBean.setUserId(TestDataManager.getRandomIntNum(8));
//			conversioncouponCall.setData(conversioncouponBean);
//			conversioncouponCall.callService();
			
			conversioncouponBean.setUserId(TestDataManager.getRandomIntNum(8));
			conversioncouponCall.setData(conversioncouponBean);
			conversioncouponCall.callService();	
			VerifyReturn(createCouponBean,createBean);
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ConversionCoupon_003() {
		Logger.start(true,"兑换优惠券-自定义券-指定新用户的批次，老用户兑换");
		try {
			//生成批次及兑换码
			CouponServiceMCallService couponCallService = new CouponServiceMCallService();
			CreateCouponBean createCouponBean = couponCallService.setDefaultPlatformCreateCouponBeanForConversion();
			createCouponBean.setUserType(1);

			CreateCouponCall createCouponCall = couponCallService.createPlatformCoupon(createCouponBean);

			CouponServiceMCallService mCouponserviceCallService = new CouponServiceMCallService();

			List<String> batchCodes = new ArrayList<>();
			batchCodes.add(createCouponCall.getBatchCode());

			CreateBean createBean = mCouponserviceCallService.setDefaultCustomCreateBean(batchCodes);
			CreateCall createCall = mCouponserviceCallService.create(createBean);
		
			// 等待兑换码生成
			YmtCouponConversionBill bill = new YmtCouponConversionBillWapper()
					.selectByBillName(createBean.getBillName()).get(0);
			List<YmtCouponConversionCode> ymtCouponConversionCodes = new YmtCouponConversionCodeWapper().selectByConversionBillId(bill.getConversionbillid());
			
			int flag=0;
			while(ymtCouponConversionCodes.size()<1 && flag<500)
			{
				flag++;
				Thread.sleep(200);
				ymtCouponConversionCodes = new YmtCouponConversionCodeWapper().selectByConversionBillId(bill.getConversionbillid());	
			}
			
			Logger.verifyEquals(createBean.getGenerateQuantity(), ymtCouponConversionCodes.size(), "验证数据Ymt_CouponConversionCode中生成相应记录");
			
			// 设置值
			conversioncouponBean.setUserId(Integer.valueOf(EnvSetup.getData("BuyUserId")));
			conversioncouponBean.setConversionCode(ymtCouponConversionCodes.get(0).getConversioncode());
			conversioncouponBean.setAppId("Ymt");
			conversioncouponBean.setRequestId("12344");
			conversioncouponBean.setUserType(2);
			
			conversioncouponCall.setData(conversioncouponBean);
			conversioncouponCall.callService();	
			
			   Logger.verifyEquals("0", conversioncouponCall.getCode(), "验证返回code");
		        Logger.verifyEquals("操作成功！", conversioncouponCall.getMessage(), "验证返回Message");

		        // 验证返回
		        Logger.verifyEquals("false", conversioncouponCall.getConversionSuccess(),
		                "验证返回IsReceiveSuccess(");
		        Logger.verifyEquals("20", conversioncouponCall.getConversionFailReason(),
		                "验证返回ReceiveFailReason-定义参考http://172.16.22.142:83/Help/ResourceModel?modelName=ReceiveFailReasonInfo");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ConversionCoupon_004() {
		Logger.start(true,"兑换优惠券-自定义券-指定老用户的批次，新用户兑换");
		try {
			//生成批次及兑换码
			CouponServiceMCallService couponCallService = new CouponServiceMCallService();
			CreateCouponBean createCouponBean = couponCallService.setDefaultPlatformCreateCouponBeanForConversion();
			createCouponBean.setUserType(2);

			CreateCouponCall createCouponCall = couponCallService.createPlatformCoupon(createCouponBean);

			CouponServiceMCallService mCouponserviceCallService = new CouponServiceMCallService();

			List<String> batchCodes = new ArrayList<>();
			batchCodes.add(createCouponCall.getBatchCode());

			CreateBean createBean = mCouponserviceCallService.setDefaultCustomCreateBean(batchCodes);
			CreateCall createCall = mCouponserviceCallService.create(createBean);
		
			// 等待兑换码生成
			YmtCouponConversionBill bill = new YmtCouponConversionBillWapper()
					.selectByBillName(createBean.getBillName()).get(0);
			List<YmtCouponConversionCode> ymtCouponConversionCodes = new YmtCouponConversionCodeWapper().selectByConversionBillId(bill.getConversionbillid());
			
			int flag=0;
			while(ymtCouponConversionCodes.size()<1 && flag<500)
			{
				flag++;
				Thread.sleep(200);
				ymtCouponConversionCodes = new YmtCouponConversionCodeWapper().selectByConversionBillId(bill.getConversionbillid());	
			}
			
			Logger.verifyEquals(createBean.getGenerateQuantity(), ymtCouponConversionCodes.size(), "验证数据Ymt_CouponConversionCode中生成相应记录");
			
			// 设置值
			conversioncouponBean.setUserId(Integer.valueOf(EnvSetup.getData("BuyUserId")));
			conversioncouponBean.setConversionCode(ymtCouponConversionCodes.get(0).getConversioncode());
			conversioncouponBean.setAppId("Ymt");
			conversioncouponBean.setRequestId("12344");
			conversioncouponBean.setUserType(1);
			
			conversioncouponCall.setData(conversioncouponBean);
			conversioncouponCall.callService();	
			
			   Logger.verifyEquals("0", conversioncouponCall.getCode(), "验证返回code");
		        Logger.verifyEquals("操作成功！", conversioncouponCall.getMessage(), "验证返回Message");

		        // 验证返回
		        Logger.verifyEquals("false", conversioncouponCall.getConversionSuccess(),
		                "验证返回IsReceiveSuccess(");
		        Logger.verifyEquals("21", conversioncouponCall.getConversionFailReason(),
		                "验证返回ReceiveFailReason-定义参考http://172.16.22.142:83/Help/ResourceModel?modelName=ReceiveFailReasonInfo");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ConversionCoupon_005() {
		Logger.start(true,"兑换优惠券-自定义券-兑换码不存在");
		try {
			
			// 设置值
			conversioncouponBean.setUserId(Integer.valueOf(EnvSetup.getData("BuyUserId")));
			conversioncouponBean.setConversionCode("asdd");
			conversioncouponBean.setAppId("Ymt");
			conversioncouponBean.setRequestId("12344");
			conversioncouponBean.setUserType(1);
			
			conversioncouponCall.setData(conversioncouponBean);
			conversioncouponCall.callService();	
			
			   Logger.verifyEquals("0", conversioncouponCall.getCode(), "验证返回code");
		        Logger.verifyEquals("操作成功！", conversioncouponCall.getMessage(), "验证返回Message");

		        // 验证返回
		        Logger.verifyEquals("false", conversioncouponCall.getConversionSuccess(),
		                "验证返回IsReceiveSuccess(");
		        Logger.verifyEquals("15", conversioncouponCall.getConversionFailReason(),
		                "验证返回ReceiveFailReason-定义参考http://172.16.22.142:83/Help/ResourceModel?modelName=ReceiveFailReasonInfo");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ConversionCoupon_006() {
		Logger.start(true,"兑换优惠券-自定义券-兑换码空");
		try {
			
			// 设置值
			conversioncouponBean.setUserId(Integer.valueOf(EnvSetup.getData("BuyUserId")));
		
			conversioncouponBean.setAppId("Ymt");
			conversioncouponBean.setRequestId("12344");
			conversioncouponBean.setUserType(1);
			
			conversioncouponCall.setData(conversioncouponBean);
			conversioncouponCall.callService();	
			
			   Logger.verifyEquals("1", conversioncouponCall.getCode(), "验证返回code");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ConversionCoupon_007() {
		Logger.start(true,"兑换优惠券-自定义券-userId不存在-服务端不做判断，userid落0");
		try {
			
			//生成批次及兑换码
			CouponServiceMCallService couponCallService = new CouponServiceMCallService();
			CreateCouponBean createCouponBean = couponCallService.setDefaultPlatformCreateCouponBeanForConversion();
			CreateCouponCall createCouponCall = couponCallService.createPlatformCoupon(createCouponBean);

			CouponServiceMCallService mCouponserviceCallService = new CouponServiceMCallService();

			List<String> batchCodes = new ArrayList<>();
			batchCodes.add(createCouponCall.getBatchCode());

			CreateBean createBean = mCouponserviceCallService.setDefaultCustomCreateBean(batchCodes);
			CreateCall createCall = mCouponserviceCallService.create(createBean);
		
			// 等待兑换码生成
			YmtCouponConversionBill bill = new YmtCouponConversionBillWapper()
					.selectByBillName(createBean.getBillName()).get(0);
			List<YmtCouponConversionCode> ymtCouponConversionCodes = new YmtCouponConversionCodeWapper().selectByConversionBillId(bill.getConversionbillid());
			
			int flag=0;
			while(ymtCouponConversionCodes.size()<1 && flag<500)
			{
				flag++;
				Thread.sleep(200);
				ymtCouponConversionCodes = new YmtCouponConversionCodeWapper().selectByConversionBillId(bill.getConversionbillid());	
			}
			
			Logger.verifyEquals(createBean.getGenerateQuantity(), ymtCouponConversionCodes.size(), "验证数据Ymt_CouponConversionCode中生成相应记录");
			
			// 设置值
			conversioncouponBean.setConversionCode(ymtCouponConversionCodes.get(0).getConversioncode());
			conversioncouponBean.setAppId("Ymt");
			conversioncouponBean.setRequestId("12344");
			conversioncouponBean.setUserType(0);
			
			conversioncouponCall.setData(conversioncouponBean);
			conversioncouponCall.callService();	
			
			   Logger.verifyEquals("0", conversioncouponCall.getCode(), "验证返回code");
		        Logger.verifyEquals("操作成功！", conversioncouponCall.getMessage(), "验证返回Message");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ConversionCoupon_008() {
		Logger.start(true,"兑换优惠券-自定义券-超出领取数");
		try {
			//生成批次及兑换码
			CouponServiceMCallService couponCallService = new CouponServiceMCallService();
			CreateCouponBean createCouponBean = couponCallService.setDefaultPlatformCreateCouponBeanForConversion();

			CreateCouponCall createCouponCall = couponCallService.createPlatformCoupon(createCouponBean);

			CouponServiceMCallService mCouponserviceCallService = new CouponServiceMCallService();

			List<String> batchCodes = new ArrayList<>();
			batchCodes.add(createCouponCall.getBatchCode());

			CreateBean createBean = mCouponserviceCallService.setDefaultCustomCreateBean(batchCodes);
			CreateCall createCall = mCouponserviceCallService.create(createBean);
		
			// 等待兑换码生成
			YmtCouponConversionBill bill = new YmtCouponConversionBillWapper()
					.selectByBillName(createBean.getBillName()).get(0);
			List<YmtCouponConversionCode> ymtCouponConversionCodes = new YmtCouponConversionCodeWapper().selectByConversionBillId(bill.getConversionbillid());
			
			int flag=0;
			while(ymtCouponConversionCodes.size()<1 && flag<500)
			{
				flag++;
				Thread.sleep(200);
				ymtCouponConversionCodes = new YmtCouponConversionCodeWapper().selectByConversionBillId(bill.getConversionbillid());	
			}
			
			Logger.verifyEquals(createBean.getGenerateQuantity(), ymtCouponConversionCodes.size(), "验证数据Ymt_CouponConversionCode中生成相应记录");
			
			// 设置值
			conversioncouponBean.setUserId(Integer.valueOf(EnvSetup.getData("BuyUserId")));
			conversioncouponBean.setConversionCode(ymtCouponConversionCodes.get(0).getConversioncode());
			conversioncouponBean.setAppId("Ymt");
			conversioncouponBean.setRequestId("12344");
			conversioncouponBean.setUserType(0);
			
			conversioncouponCall.setData(conversioncouponBean);
			conversioncouponCall.callService();	
			
			conversioncouponCall.setData(conversioncouponBean);
			conversioncouponCall.callService();	
			
			Logger.verifyEquals("0", conversioncouponCall.getCode(), "验证返回code");
		    Logger.verifyEquals("操作成功！", conversioncouponCall.getMessage(), "验证返回Message");

		        // 验证返回
		    Logger.verifyEquals("false", conversioncouponCall.getConversionSuccess(),
		                "验证返回IsReceiveSuccess(");
		    Logger.verifyEquals("2", conversioncouponCall.getConversionFailReason(),
		                "验证返回ReceiveFailReason-定义参考http://172.16.22.142:83/Help/ResourceModel?modelName=ReceiveFailReasonInfo");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ConversionCoupon_009() {
		Logger.start(true,"兑换优惠券-自定义券-已过期");
		try {
			//生成批次及兑换码
			CouponServiceMCallService couponCallService = new CouponServiceMCallService();
			CreateCouponBean createCouponBean = couponCallService.setDefaultPlatformCreateCouponBeanForConversion();

			CreateCouponCall createCouponCall = couponCallService.createPlatformCoupon(createCouponBean);

			CouponServiceMCallService mCouponserviceCallService = new CouponServiceMCallService();

			List<String> batchCodes = new ArrayList<>();
			batchCodes.add(createCouponCall.getBatchCode());

			CreateBean createBean = mCouponserviceCallService.setDefaultCustomCreateBean(batchCodes);
			CreateCall createCall = mCouponserviceCallService.create(createBean);
		
			// 等待兑换码生成
			YmtCouponConversionBill bill = new YmtCouponConversionBillWapper()
					.selectByBillName(createBean.getBillName()).get(0);
			List<YmtCouponConversionCode> ymtCouponConversionCodes = new YmtCouponConversionCodeWapper().selectByConversionBillId(bill.getConversionbillid());
			
			int flag=0;
			while(ymtCouponConversionCodes.size()<1 && flag<500)
			{
				flag++;
				Thread.sleep(200);
				ymtCouponConversionCodes = new YmtCouponConversionCodeWapper().selectByConversionBillId(bill.getConversionbillid());	
			}
			
			Logger.verifyEquals(createBean.getGenerateQuantity(), ymtCouponConversionCodes.size(), "验证数据Ymt_CouponConversionCode中生成相应记录");
			
			YmtCouponBatch batch = new YmtCouponBatchWapper().selectBysBatchCode(createCouponCall.getBatchCode()).get(0);
			
			Date dNow = new Date(); // 当前时间
			
			  Calendar calendarstart = Calendar.getInstance(); // 得到日历
		        calendarstart.setTime(dNow);// 把当前时间赋给日历
		        calendarstart.add(Calendar.DAY_OF_MONTH, -10); // 设置为前一天
		        Date dStart = calendarstart.getTime(); // 得到前一天的时间

		        Calendar calendarend = Calendar.getInstance(); // 得到日历
		        calendarend.setTime(dNow);// 把当前时间赋给日历
		        calendarend.add(Calendar.DAY_OF_MONTH, -1); // 设置为后十天
		        Date dEnd = calendarend.getTime(); // 得到前一天的时间
		        
			new YmtCouponSettingWapper().setdValidStart(batch.getiCouponSettingId(), dStart);
			new YmtCouponSettingWapper().setdValidEnd(batch.getiCouponSettingId(), dEnd);
			
			// 设置值
			conversioncouponBean.setUserId(Integer.valueOf(EnvSetup.getData("BuyUserId")));
			conversioncouponBean.setConversionCode(ymtCouponConversionCodes.get(0).getConversioncode());
			conversioncouponBean.setAppId("Ymt");
			conversioncouponBean.setRequestId("12344");
			conversioncouponBean.setUserType(0);
			
			conversioncouponCall.setData(conversioncouponBean);
			conversioncouponCall.callService();				
			
			Logger.verifyEquals("0", conversioncouponCall.getCode(), "验证返回code");
		    Logger.verifyEquals("操作成功！", conversioncouponCall.getMessage(), "验证返回Message");

		        // 验证返回
		    Logger.verifyEquals("false", conversioncouponCall.getConversionSuccess(),
		                "验证返回IsReceiveSuccess(");
		    Logger.verifyEquals("3", conversioncouponCall.getConversionFailReason(),
		                "验证返回ReceiveFailReason-定义参考http://172.16.22.142:83/Help/ResourceModel?modelName=ReceiveFailReasonInfo");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ConversionCoupon_010() {
		Logger.start(true,"兑换优惠券-自定义券-已作废");
		try {
			//生成批次及兑换码
			CouponServiceMCallService couponCallService = new CouponServiceMCallService();
			CreateCouponBean createCouponBean = couponCallService.setDefaultPlatformCreateCouponBeanForConversion();

			CreateCouponCall createCouponCall = couponCallService.createPlatformCoupon(createCouponBean);

			CouponServiceMCallService mCouponserviceCallService = new CouponServiceMCallService();

			List<String> batchCodes = new ArrayList<>();
			batchCodes.add(createCouponCall.getBatchCode());

			CreateBean createBean = mCouponserviceCallService.setDefaultCustomCreateBean(batchCodes);
			CreateCall createCall = mCouponserviceCallService.create(createBean);
		
			// 等待兑换码生成
			YmtCouponConversionBill bill = new YmtCouponConversionBillWapper()
					.selectByBillName(createBean.getBillName()).get(0);
			List<YmtCouponConversionCode> ymtCouponConversionCodes = new YmtCouponConversionCodeWapper().selectByConversionBillId(bill.getConversionbillid());
			
			int flag=0;
			while(ymtCouponConversionCodes.size()<1 && flag<500)
			{
				flag++;
				Thread.sleep(200);
				ymtCouponConversionCodes = new YmtCouponConversionCodeWapper().selectByConversionBillId(bill.getConversionbillid());	
			}
			
			Logger.verifyEquals(createBean.getGenerateQuantity(), ymtCouponConversionCodes.size(), "验证数据Ymt_CouponConversionCode中生成相应记录");
			
			new CouponServiceMCallService().InvalidPlatformBatch(createCouponCall.getBatchCode(), "1223", "33fdff");
			// 设置值
			conversioncouponBean.setUserId(Integer.valueOf(EnvSetup.getData("BuyUserId")));
			conversioncouponBean.setConversionCode(ymtCouponConversionCodes.get(0).getConversioncode());
			conversioncouponBean.setAppId("Ymt");
			conversioncouponBean.setRequestId("12344");
			conversioncouponBean.setUserType(0);
			
			conversioncouponCall.setData(conversioncouponBean);
			conversioncouponCall.callService();				
			
			Logger.verifyEquals("0", conversioncouponCall.getCode(), "验证返回code");
		    Logger.verifyEquals("操作成功！", conversioncouponCall.getMessage(), "验证返回Message");

		        // 验证返回
		    Logger.verifyEquals("false", conversioncouponCall.getConversionSuccess(),
		                "验证返回IsReceiveSuccess(");
		    Logger.verifyEquals("4", conversioncouponCall.getConversionFailReason(),
		                "验证返回ReceiveFailReason-定义参考http://172.16.22.142:83/Help/ResourceModel?modelName=ReceiveFailReasonInfo");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ConversionCoupon_011() {
		Logger.start(true,"兑换优惠券-自定义券-批次不存在");
		try {
			//生成批次及兑换码
			CouponServiceMCallService couponCallService = new CouponServiceMCallService();
			CreateCouponBean createCouponBean = couponCallService.setDefaultPlatformCreateCouponBeanForConversion();

			CreateCouponCall createCouponCall = couponCallService.createPlatformCoupon(createCouponBean);

			CouponServiceMCallService mCouponserviceCallService = new CouponServiceMCallService();

			List<String> batchCodes = new ArrayList<>();
			batchCodes.add(createCouponCall.getBatchCode());

			CreateBean createBean = mCouponserviceCallService.setDefaultCustomCreateBean(batchCodes);
			CreateCall createCall = mCouponserviceCallService.create(createBean);
		
			// 等待兑换码生成
			YmtCouponConversionBill bill = new YmtCouponConversionBillWapper()
					.selectByBillName(createBean.getBillName()).get(0);
			List<YmtCouponConversionCode> ymtCouponConversionCodes = new YmtCouponConversionCodeWapper().selectByConversionBillId(bill.getConversionbillid());
			
			int flag=0;
			while(ymtCouponConversionCodes.size()<1 && flag<500)
			{
				flag++;
				Thread.sleep(200);
				ymtCouponConversionCodes = new YmtCouponConversionCodeWapper().selectByConversionBillId(bill.getConversionbillid());	
			}
			
			Logger.verifyEquals(createBean.getGenerateQuantity(), ymtCouponConversionCodes.size(), "验证数据Ymt_CouponConversionCode中生成相应记录");
			
			new YmtCouponConversionBatchRelationWapper().setCouponBatchCode(bill.getConversionbillid(), "999999999");
		
			// 设置值
			conversioncouponBean.setUserId(Integer.valueOf(EnvSetup.getData("BuyUserId")));
			conversioncouponBean.setConversionCode(ymtCouponConversionCodes.get(0).getConversioncode());
			conversioncouponBean.setAppId("Ymt");
			conversioncouponBean.setRequestId("12344");
			conversioncouponBean.setUserType(0);
			
			conversioncouponCall.setData(conversioncouponBean);
			conversioncouponCall.callService();				
			
			Logger.verifyEquals("0", conversioncouponCall.getCode(), "验证返回code");
		    Logger.verifyEquals("操作成功！", conversioncouponCall.getMessage(), "验证返回Message");

		        // 验证返回
		    Logger.verifyEquals("false", conversioncouponCall.getConversionSuccess(),
		                "验证返回IsReceiveSuccess(");
		    Logger.verifyEquals("8", conversioncouponCall.getConversionFailReason(),
		                "验证返回ReceiveFailReason-定义参考http://172.16.22.142:83/Help/ResourceModel?modelName=ReceiveFailReasonInfo");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ConversionCoupon_012() {
		Logger.start(true,"兑换优惠券-自定义券-批次不能用于兑换");
		try {
			//生成批次及兑换码
			CouponServiceMCallService couponCallService = new CouponServiceMCallService();
			CreateCouponBean createCouponBean = couponCallService.setDefaultPlatformCreateCouponBeanForRecieve();

			CreateCouponCall createCouponCall = couponCallService.createPlatformCoupon(createCouponBean);

			CouponServiceMCallService mCouponserviceCallService = new CouponServiceMCallService();

			List<String> batchCodes = new ArrayList<>();
			batchCodes.add(createCouponCall.getBatchCode());

			CreateBean createBean = mCouponserviceCallService.setDefaultCustomCreateBean(batchCodes);
			CreateCall createCall = mCouponserviceCallService.create(createBean);
		
			// 等待兑换码生成
			YmtCouponConversionBill bill = new YmtCouponConversionBillWapper()
					.selectByBillName(createBean.getBillName()).get(0);
			List<YmtCouponConversionCode> ymtCouponConversionCodes = new YmtCouponConversionCodeWapper().selectByConversionBillId(bill.getConversionbillid());
			
			int flag=0;
			while(ymtCouponConversionCodes.size()<1 && flag<500)
			{
				flag++;
				Thread.sleep(200);
				ymtCouponConversionCodes = new YmtCouponConversionCodeWapper().selectByConversionBillId(bill.getConversionbillid());	
			}
			
			Logger.verifyEquals(createBean.getGenerateQuantity(), ymtCouponConversionCodes.size(), "验证数据Ymt_CouponConversionCode中生成相应记录");

			// 设置值
			conversioncouponBean.setUserId(Integer.valueOf(EnvSetup.getData("BuyUserId")));
			conversioncouponBean.setConversionCode(ymtCouponConversionCodes.get(0).getConversioncode());
			conversioncouponBean.setAppId("Ymt");
			conversioncouponBean.setRequestId("12344");
			conversioncouponBean.setUserType(0);
			
			conversioncouponCall.setData(conversioncouponBean);
			conversioncouponCall.callService();				
			
			Logger.verifyEquals("0", conversioncouponCall.getCode(), "验证返回code");
		    Logger.verifyEquals("操作成功！", conversioncouponCall.getMessage(), "验证返回Message");

		        // 验证返回
		    Logger.verifyEquals("false", conversioncouponCall.getConversionSuccess(),
		                "验证返回IsReceiveSuccess(");
		    Logger.verifyEquals("18", conversioncouponCall.getConversionFailReason(),
		                "验证返回ReceiveFailReason-定义参考http://172.16.22.142:83/Help/ResourceModel?modelName=ReceiveFailReasonInfo");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ConversionCoupon_013() {
		Logger.start(true,"兑换优惠券-兑换码不存在");
		try {
			// 设置值
			conversioncouponBean.setUserId(Integer.valueOf(EnvSetup.getData("BuyUserId")));
			conversioncouponBean.setConversionCode("dddeewe");
			conversioncouponBean.setAppId("Ymt");
			conversioncouponBean.setRequestId("12344");
			conversioncouponBean.setUserType(0);
			
			conversioncouponCall.setData(conversioncouponBean);
			conversioncouponCall.callService();				
			
			Logger.verifyEquals("0", conversioncouponCall.getCode(), "验证返回code");
		    Logger.verifyEquals("操作成功！", conversioncouponCall.getMessage(), "验证返回Message");

		        // 验证返回
		    Logger.verifyEquals("false", conversioncouponCall.getConversionSuccess(),
		                "验证返回IsReceiveSuccess(");
		    Logger.verifyEquals("15", conversioncouponCall.getConversionFailReason(),
		                "验证返回ReceiveFailReason-定义参考http://172.16.22.142:83/Help/ResourceModel?modelName=ReceiveFailReasonInfo");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	private void VerifyReturn(CreateCouponBean createcouponBean,CreateBean createBean)
    {
        Logger.verifyEquals("0", conversioncouponCall.getCode(), "验证返回code");
        Logger.verifyEquals("操作成功！", conversioncouponCall.getMessage(), "验证返回Message");

        // 验证返回
        Logger.verifyEquals("true", conversioncouponCall.getConversionSuccess(),
                "验证返回IsReceiveSuccess(");
        Logger.verifyEquals("0", conversioncouponCall.getConversionFailReason(),
                "验证返回ReceiveFailReason");
        
        // 验证表Ymt_CouponConversionRecord
        YmtCouponConversionBill bill = new YmtCouponConversionBillWapper()
				.selectByBillName(createBean.getBillName()).get(0);
        
        List<YmtCouponConversionRecord> records = new YmtCouponConversionRecordWapper().selectByConversionBillId(bill.getConversionbillid());
        Logger.verifyEquals(1, records.size(), "验证数据库表YmtCouponConversionRecord中生成条记录");
        
        if(records.size()==1)
        {
        	YmtCouponConversionRecord record = records.get(0);
        	Logger.verifyEquals(bill.getConversionbillid(), record.getConversionbillid(),
                "验证数据库表YmtCouponConversionRecord中Conversionbillid");
        	Logger.verifyEquals(conversioncouponBean.getConversionCode(), record.getConversioncode(),
                    "验证数据库表YmtCouponConversionRecord中Conversioncode");
        	Logger.verifyEquals(conversioncouponBean.getUserId(), record.getUserid(),
                    "验证数据库表YmtCouponConversionRecord中Userid");
        }
        List<String> batchCodes = createBean.getCouponBatchCodes();
        
        for (String batchCode : batchCodes) {		
		
        YmtCouponBatch couponBatch = new YmtCouponBatchWapper().selectBysBatchCode(batchCode).get(0);
        YmtCouponValue couponValue =
                new YmtCouponValueWapper().selectByiCouponSettingId(couponBatch.getiCouponSettingId()).get(0);
        YmtCouponSetting couponSetting =
                new YmtCouponSettingWapper().selectByiCouponSettingId(couponBatch.getiCouponSettingId()).get(0);
        YmtCouponScenario couponScenario =
                new YmtCouponScenarioWapper().selectByiCouponScenarioId(couponSetting.getiScenarioId()).get(0);
        YmtCoupon coupon = new YmtCouponWapper().selectByiBatchId(couponBatch.getiBatchId()).get(0);
        YmtCouponPrivateUserBound couponPrivateUserBound =
                new YmtCouponPrivateUserBoundWapper().selectBysCouponCode(coupon.getsCouponCode()).get(0);
   
        // 验证数据库
        Logger.verifyEquals(conversioncouponBean.getUserId(), couponPrivateUserBound.getiUserId(),
                "验证数据库表Ymt_CouponPrivateUserBound中iUserId");
        Logger.verifyEquals(batchCode, couponBatch.getsBatchCode(),
                "验证数据库表Ymt_CouponBatch中BatchCode");
        Logger.verifyEquals(couponPrivateUserBound.getiBatchId(), couponBatch.getiBatchId(),
                "验证数据库表Ymt_CouponBatch与表Ymt_CouponPrivateUserBound中iBatchId");
        
        Logger.verifyEquals(1, couponSetting.getiReceiveCount(), "验证数据库表Ymt_CouponSetting中iReceiveCount");

        List<YmtCouponPrivateUserBound> bounds = new YmtCouponPrivateUserBoundWapper().selectByBatchIdAndUserId(couponBatch.getiBatchId(), conversioncouponBean.getUserId());
        Logger.verifyEquals(1, bounds.size(), "验证数据库表YmtCouponPrivateUserBound中生成条记录");
        Logger.verifyEquals(coupon.getsCouponCode(), bounds.get(0).getsCouponCode(), "验证数据库表YmtCouponPrivateUserBound中生成条记录");
        Logger.verifyEquals(coupon.getsCouponCode(), conversioncouponCall.getCouponCodes().get(0), "验证返回CouponCodes");
        
        }
    }
}